# coding = utf-8

'''
读取数据的一个标准方法
'''

import os
import SimpleITK as sitk
import numpy as np


def read_data():
    predict_root_path = "E:\predict\gabor_class_instance8"
    raw_root_path = "E:\Dataset\Qiye"
    #raw_root_path = "F:\Dataset\Liver\qiye"

    raw_id_list = []
    for item in sorted(os.listdir(os.path.join(raw_root_path, "DongBeiDaXue\liver"))):
        raw_id_list.append(item.split("_")[1])
    for item in sorted(os.listdir(os.path.join(raw_root_path, "DongBeiDaXue2\liver"))):
        raw_id_list.append(item.split("_")[1])


    for i in range(80):
        case_id = "case_{}".format(str(i).zfill(5))
        predict_liver_path = os.path.join(predict_root_path, "{}\label_liver".format(case_id))
        liver_num = len(os.listdir(predict_liver_path))

        if i < 50:
            raw_liver_file = os.path.join(raw_root_path, "DongBeiDaXue\liver\\data2_{}_liver_label.mha".format(raw_id_list[i]))
        else:
            raw_liver_file = os.path.join(raw_root_path, "DongBeiDaXue2\liver\\data2_{}_liver_label.mha".format(raw_id_list[i]))

        liver = sitk.GetArrayFromImage(sitk.ReadImage(raw_liver_file))
        liver_num_cpoy = 0
        for t in range(liver.shape[0]):
             if np.max(liver[t]) > 0:
                 liver_num_cpoy += 1

        print(case_id, liver_num, liver_num_cpoy)
        assert liver_num == liver_num_cpoy, "case_id:{}, raw_data:{}".format(case_id, raw_liver_file)


def analysis_predict():
    predict_root_path = "E:\predict\gabor_class_instance8"
    for i in range(80):
        case_id = "case_{}".format(str(i).zfill(5))
        predict_numpy_path = os.path.join(predict_root_path, "{}\predict_numpy".format(case_id))
        prob_numpy_path = os.path.join(predict_root_path, "{}\prob_numpy".format(case_id))
        for item in sorted(os.listdir(predict_numpy_path)):
            predict_numpy_file = os.path.join(predict_numpy_path, item)
            predict_numpy = np.load(predict_numpy_file)
            break
        for item in sorted(os.listdir(prob_numpy_path)):
            prob_numpy_file = os.path.join(prob_numpy_path, item)
            prob_numpy = np.load(prob_numpy_file)
            break
        print("predict", predict_numpy.shape, np.min(predict_numpy), np.max(predict_numpy))
        print("prob", prob_numpy.shape, np.min(prob_numpy), np.max(prob_numpy))







if __name__ == '__main__':
    analysis_predict()